from django.conf import settings
import re

# Check, if environment is set properly
import api

GAE_CACHE_RE = re.compile(''.join(["( href| src)=([\'\"])", settings.MEDIA_URL, "(.*?)[\'\"]"]))
GAE_CACHE_REPLACEMENT = ''.join([r' \1=\2', settings.GAE_CACHE_URL, settings.MEDIA_URL, r'\3\2'])

class DjangoGaeCacheMiddleware(object):
    def process_response(self, request, response):
        if settings.GAE_CACHE_USE:
            response.content = GAE_CACHE_RE.sub(GAE_CACHE_REPLACEMENT, response.content)
        return response
